/**
 * OperationReportServiceImpl.java
 * Created at 2017-11-28
 * Created by zhanghailin
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.broadtext.common.exp.DaoException;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.common.repository.EntityDao;
import com.broadtext.common.service.impl.EntityServiceImpl;
import com.broadtext.common.utils.StringUtils;
import com.broadtext.rms.system.model.OperationReport;
import com.broadtext.rms.system.service.IOperationReportService;

/**
 * <p>ClassName: OperationReportServiceImpl</p>
 * <p>Description: TODO</p>
 * <p>Author: zhanghailin</p>
 * <p>Date: 2017-11-28</p>
 */
@Service("operationReportService")
public class OperationReportServiceImpl extends EntityServiceImpl<OperationReport> implements IOperationReportService{

    @Autowired
    @Qualifier("operationReportDao")
    public void setEntityDao(EntityDao<OperationReport> entityDao) {
        super.entityDao = entityDao;
    }
    
    
    /** (non-Javadoc)
     * <p>Title: getAlertData</p>
     * <p>Description: 方法描述，主要是获取rms相关的方法介绍</p>
     * @return
     * @throws ServiceException
     * @see com.broadtext.rms.system.service.IOperationReportService#getAlertData()
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Object> getAlertData(Map<String, Object> paramsMap) throws ServiceException {
        List<Object> list = new ArrayList<Object>();
        try {
            list = this.entityDao.findByProperty("com.broadtext.rms.system.model.OperationReport.getAlertData", paramsMap);
        } catch (DaoException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }


    /** (non-Javadoc)
     * <p>Title: getAlertPieChartData</p>
     * <p>Description: </p>
     * @return
     * @throws ServiceException
     * @see com.broadtext.rms.system.service.IOperationReportService#getAlertPieChartData()
     */
    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> getAlertPieChartData(Map<String, Object> paramsMap) throws ServiceException {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        List<Object> legendData = new ArrayList<Object>();
        try {
            list = this.entityDao.findByProperty("com.broadtext.rms.system.model.OperationReport.getAlertPieChartData", paramsMap);
            map.put("seriesData", list);
            if(list!=null&&list.size()>0){
                for (Map<String, Object> map2 : list) {
                    legendData.add(map2.get("name"));
                }
            }
            map.put("legendData", legendData);
        } catch (DaoException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return map;
    }


    /** (non-Javadoc)
     * <p>Title: getSaleSupportChartData</p>
     * <p>Description: </p>
     * @return
     * @throws ServiceException
     * @see com.broadtext.rms.system.service.IOperationReportService#getSaleSupportChartData()
     */
    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> getSaleSupportChartData(Map<String, Object> paramsMap) throws ServiceException {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        List<Object> reqLegendData = new ArrayList<Object>();
        List<Object> woLegendData = new ArrayList<Object>();
        try {
            //查询服务请求饼图数据
            list = this.entityDao.findByProperty("com.broadtext.rms.system.model.OperationReport.getServiceReqChartData", paramsMap);
            map.put("reqSeriesData", list);
            if(list!=null&&list.size()>0){
                for (Map<String, Object> map2 : list) {
                    reqLegendData.add(map2.get("name"));
                }
            }
            map.put("reqLegendData", reqLegendData);
            String reqTotalNumber = this.getQueryResult("com.broadtext.rms.system.model.OperationReport.getServiceReqTotalNum", paramsMap);
            map.put("reqTotalNumber", reqTotalNumber);
            //查询服务工单饼图数据
            list = this.entityDao.findByProperty("com.broadtext.rms.system.model.OperationReport.getWoChartData", paramsMap);
            map.put("woSeriesData", list);
            if(list!=null&&list.size()>0){
                for (Map<String, Object> map2 : list) {
                    woLegendData.add(map2.get("name"));
                }
            }
            map.put("woLegendData", woLegendData);
            String woTotalNumber = this.getQueryResult("com.broadtext.rms.system.model.OperationReport.getWoTotalNum", paramsMap);
            map.put("woTotalNumber", woTotalNumber);
        } catch (DaoException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return map;
    }


    /** (non-Javadoc)
     * <p>Title: getCustomerOpt</p>
     * <p>Description: </p>
     * @param customerId
     * @return
     * @throws ServiceException
     * @see com.broadtext.rms.system.service.IOperationReportService#getCustomerOpt(java.lang.String)
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Object> getCustomerOpt(String customerId) throws ServiceException {
        List<Object> list;
        list = new ArrayList<Object>();
        List<Map<String,Object>> cusIdList;
        cusIdList = new ArrayList<Map<String,Object>>();
        Map<String, Object> queryMap;
        queryMap = new HashMap<String, Object>();
        List<String> ids = new ArrayList<String>() ;
        if(StringUtils.isNotBlank(customerId)){
            queryMap.put("rootId", customerId);
        }
        try {
            //查询本级和本下级客户id
            cusIdList = this.entityDao.findByProperty("com.broadtext.rms.basic.model.TmCustom.findTreeCusIds", queryMap);
            for (Map<String,Object> map : cusIdList) {
                ids.add(map.get("PK_ID")+"");
            }
            list = this.entityDao.findByProperty("com.broadtext.rms.system.model.OperationReport.getChildCusOption", ids);
        } catch (DaoException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
        return list;
    }


    /** (non-Javadoc)
     * <p>Title: getBatteryBasicInfo</p>
     * <p>Description: 获取报告中电池基础信息</p>
     * @return
     * @throws ServiceException
     * @see com.broadtext.rms.system.service.IOperationReportService#getBatteryBasicInfo()
     */
    @Override
    public Map<String, Object> getBatteryBasicInfo(Map<String, Object> queryMap) throws ServiceException {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        //查询客户名称
        String customerName = "全部";
        if(queryMap.get("customerId")!=null){
            customerName = this.getQueryResult("com.broadtext.rms.system.model.OperationReport.getCustomerName", queryMap);
        }
        dataMap.put("customerName", customerName);
        //查询电池总数
        String batteryNum = this.getQueryResult("com.broadtext.rms.system.model.OperationReport.getBatteryNum", queryMap);
        dataMap.put("batteryNum", batteryNum);
        //查询上过电的电池数
        String onPowerNum = this.getQueryResult("com.broadtext.rms.system.model.OperationReport.getOnPowerNum", queryMap);
        dataMap.put("onPowerNum", onPowerNum);
        //查询报警数
        String alertNum = this.getQueryResult("com.broadtext.rms.system.model.OperationReport.getTotalAlertNum", queryMap);
        dataMap.put("alertNum", alertNum);
        //查询累计运行时间
        String totalRunTime = this.getQueryResult("com.broadtext.rms.system.model.OperationReport.getTotalRunTime", queryMap);
        dataMap.put("totalRunTime", totalRunTime);
        return dataMap;
    }
    
    @SuppressWarnings("unchecked")
    private String getQueryResult(String sqlAlias, Map<String, Object> queryMap) {
        List<Object> list = new ArrayList<Object>();
        try {
            list = this.entityDao.findByProperty(sqlAlias, queryMap);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        if(list!=null&&list.size()>0){
            return String.valueOf(list.get(0));
        }
        return "";
    }

}
